package com.jasen.springcloud.controller;

import com.jasen.springcloud.entities.Dept;
import com.jasen.springcloud.service.DeptService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @HystrixCommand 熔断机制的注解
 */
@RestController
public class DeptController {

    @Autowired
    private DeptService service;

    @PostMapping(value="/dept/add")
    public boolean add(@RequestBody Dept dept)
    {
        return service.add(dept);
    }

    @GetMapping(value="/dept/get/{id}")
    @HystrixCommand(fallbackMethod = "processHystrix_Get")
    public Dept get(@PathVariable("id") Long id)
    {
        Dept dept = service.get(id);
        if (dept == null) {
            throw new RuntimeException("该ID："+id+"没有没有对应的信息");
        }
        return dept;
    }

    @GetMapping(value="/dept/list")
    @HystrixCommand
    public List<Dept> list()
    {
        return service.list();
    }

    /**
     * 服务熔断处理方法
     * @param id
     * @return
     */
    public Dept processHystrix_Get(@PathVariable("id") Long id) {
        return new Dept().setDeptno(id).setDname("该id" + id + "没有对应的信息，返回值Null  --@HystrixCommand");
    }

}